package com.bot.adms.controller.apply;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bot.adms.model.bo.ApplyInfoBo;
import com.bot.adms.service.IApplyService;
import com.bot.adms.service.impl.ApplyServiceImpl;
import com.bot.adms.utils.AddApplyUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/apply/add.do")
public class AddApplyServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //修改字符集
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");

        // 定义响应状态码（默认 500 服务器错误，后续根据业务逻辑修改）
        int code = 500;
        String msg = "";
        boolean success = false;

        try{
            //1.获取数据
            //1.1获取数据长度
            int contentLength = req.getContentLength();

            //1.2输入流读取数据
            ServletInputStream inputStream = req.getInputStream();

            byte[] bytes = new byte[contentLength];
            //1.3将信息读取到数组中
            inputStream.read(bytes);
            //1.4将数据装换成字符串
            String applyJson = new String(bytes, "UTF-8");

            JSONObject applyJsonObject = JSON.parseObject(applyJson);

            //2将数据转换成bo对象
            ApplyInfoBo applyInfoBo = new AddApplyUtils().getApplyInfoBo(applyJsonObject);

            //3.参数验证

            //4.调用方法
            IApplyService applyService = new ApplyServiceImpl();
            Boolean b = applyService.addApply(applyInfoBo);

            //5.返回结果
            if (b){
                code = 200;
                msg="保存成功";
                success = true;
            }else{
                code = 500;
                msg="保存失败";
                success = false;
            }
        } catch (RuntimeException e) {
            //捕获运行异常
            code = 500;//服务器错误
            msg = "服务器处理异常，请稍后再试" + e.getMessage();
            success = false;
            e.printStackTrace();//打印日志

        } catch (Exception e) {
            //捕获其他异常
            code = 500;
            msg = "系统异常，请联系服务员或重试" + e.getMessage();
            success = false;
            e.printStackTrace();//打印日志
        }finally{
            //转换成json数据发送
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", code);
            jsonObject.put("msg", msg);
            jsonObject.put("success", success);

            //将json数据转化为字符串
            String jsonString = JSON.toJSONString(jsonObject);

            //将数据返回
            ServletOutputStream outputStream = resp.getOutputStream();
            outputStream.write(jsonString.getBytes("UTF-8"));
            outputStream.flush();
            outputStream.close();
        }

    }
}
